package sandbox;

import wads.drawing.line.CaseLink;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CaseLinkDialogOld extends JDialog
{

  CaseLink   link;
  String   stereoType;
  String   cardinalityAtEndNode;
  String   cardinalityAtStartNode;
  String   label;

  JPanel   dataPanel;
  JTextField labelField;


  CaseLinkDialogOld() {
    super();

    stereoType = "";
    cardinalityAtStartNode = "";
    cardinalityAtEndNode = "";
    label = "";

    //Init panel
    dataPanel = new JPanel();

    //Init patterns in combo box
    String[] stereoTypeOptions = { "",
                     "<<Actor>>",
                     "<<Communicates>>",
                     "<<Extends>>",
                     "<<Includes>>",
                     "<<Interface>>",
                     "<<Uses>>" };
    setStereoType(stereoTypeOptions[0]);

    // Set up the UI for selecting a stereotype.
    JLabel stereoTypeLabel = new JLabel("Stereotype");
    stereoTypeLabel.setAlignmentX(Component.LEFT_ALIGNMENT);

    JComboBox stereoTypeCombo = new JComboBox(stereoTypeOptions);
    stereoTypeCombo.setEditable(false);
    stereoTypeCombo.setSelectedIndex(0);
    stereoTypeCombo.setAlignmentX(Component.LEFT_ALIGNMENT);
    StereoTypeComboListener stereoTypeListener =
      new StereoTypeComboListener();
    stereoTypeCombo.addActionListener(stereoTypeListener);

    // Set up the UI for selecting cardinalities.
    JLabel cardinalityStartLabel = new JLabel("Cardinality at start node");
    JLabel cardinalityEndLabel = new JLabel("Cardinality at end node");
    cardinalityStartLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
    cardinalityEndLabel.setAlignmentX(Component.LEFT_ALIGNMENT);

    String[] cardinalityOptions = { "", "0", "1", "0..*", "1..*", "*" };
    JComboBox cardinalityAtStartNodeCombo =
      new JComboBox(cardinalityOptions);
    cardinalityAtStartNodeCombo.setEditable(true);
    cardinalityAtStartNodeCombo.setSelectedIndex(0);
    cardinalityAtStartNodeCombo.setAlignmentX(Component.LEFT_ALIGNMENT);
    CardinalityComboListener cardinalityAtStartNodeListener =
      new CardinalityComboListener(CardinalityComboListener.START_NODE);
    cardinalityAtStartNodeCombo.addActionListener(
      cardinalityAtStartNodeListener);

    JComboBox cardinalityAtEndNodeCombo =
      new JComboBox(cardinalityOptions);
    cardinalityAtEndNodeCombo.setEditable(true);
    cardinalityAtEndNodeCombo.setSelectedIndex(0);
    cardinalityAtEndNodeCombo.setAlignmentX(Component.LEFT_ALIGNMENT);
    CardinalityComboListener cardinalityAtEndNodeListener =
      new CardinalityComboListener(CardinalityComboListener.END_NODE);
    cardinalityAtEndNodeCombo.addActionListener(
      cardinalityAtEndNodeListener);

    // Set up the UI for entering input
    JLabel labelInputLabel = new JLabel("Label");
    labelInputLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
    labelField = new JTextField("New link");
    labelField.setAlignmentX(Component.LEFT_ALIGNMENT);


    // Lay out everything
    JPanel stereoTypePanel = new JPanel();
    stereoTypePanel.setLayout(new GridLayout(2, 1));
    stereoTypePanel.add(stereoTypeLabel);
    stereoTypePanel.add(stereoTypeCombo);

    JPanel cardinalityStartPanel = new JPanel();
    cardinalityStartPanel.setLayout(new GridLayout(2, 1));
    cardinalityStartPanel.add(cardinalityStartLabel);
    cardinalityStartPanel.add(cardinalityAtStartNodeCombo);

    JPanel cardinalityEndPanel = new JPanel();
    cardinalityEndPanel.setLayout(new GridLayout(2, 1));
    cardinalityEndPanel.add(cardinalityEndLabel);
    cardinalityEndPanel.add(cardinalityAtEndNodeCombo);

    JPanel labelInputPanel = new JPanel();
    labelInputPanel.setLayout(new GridLayout(2, 1));
    labelInputPanel.add(labelInputLabel);
    labelInputPanel.add(labelField);

    dataPanel.setLayout(new GridLayout(4, 1));
    dataPanel.add(stereoTypePanel);
    dataPanel.add(cardinalityStartPanel);
    dataPanel.add(cardinalityEndPanel);
    dataPanel.add(labelInputPanel);

    dataPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));



    //Add buttons
    JPanel buttonsPanel = new JPanel();
    JButton okButton = new JButton("Ok");
    JButton cancelButton = new JButton("Cancel");
    okButton.addActionListener(
      new ActionListener()
      {
        public void actionPerformed(ActionEvent evt)
        {
          setLabel(labelField.getText());
          CaseLinkDialogOld.this.setVisible(false);
        }
      }
    );

    cancelButton.addActionListener(
      new ActionListener()
      {
        public void actionPerformed(ActionEvent evt)
        {
          CaseLinkDialogOld.this.setVisible(false);
        }

      }
    );

    buttonsPanel.add(okButton);
    buttonsPanel.add(cancelButton);

    this.getContentPane().add(buttonsPanel,"South");
    this.getContentPane().add(dataPanel,"Center");
    ///////////////////

  //    this.setSize(400,300);
  //    setResizable(true);
  //    this.setModal(true);
  }

      public void setStereoType(String stereoType) {
    this.stereoType = stereoType;
  }
  public String getStereoType() { return stereoType; }

  public void setLabel(String label) { this.label = label; }
  public String getLabel() { return label; }

  public void setCardinalityAtEndNode(String cardinalityAtEndNode) {
    this.cardinalityAtEndNode = cardinalityAtEndNode;
  }

  public void setCardinalityAtStartNode(String cardinalityAtStartNode) {
    this.cardinalityAtStartNode = cardinalityAtStartNode;
  }

  public String getCardinalityAtEndNode() {
    return cardinalityAtEndNode;
  }

  public String getCardinalityAtStartNode() {
    return cardinalityAtStartNode;
  }

      /** Listens to the cardinality combo box. */
  class StereoTypeComboListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      setStereoType((String)((JComboBox)e.getSource()).getSelectedItem());
    }
  }

  /** Listens to the cardinality combo box. */
  class CardinalityComboListener implements ActionListener {
    public final static int START_NODE = 0;
    public final static int END_NODE = 1;

    int whichEnd;

    public CardinalityComboListener(int whichEnd) {
      this.whichEnd = whichEnd;
    }

    public void actionPerformed(ActionEvent e) {
      if (whichEnd == START_NODE)
        setCardinalityAtStartNode((String)((JComboBox)e.getSource()).getSelectedItem());
      else
        setCardinalityAtEndNode((String)((JComboBox)e.getSource()).getSelectedItem());
    }
  }

}
